package org.aeon.gamechatnest.service.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import jakarta.validation.constraints.NotNull;
import org.aeon.gamechatnest.mapper.TestEntityMapper;
import org.aeon.gamechatnest.pojo.TestEntity;
import org.aeon.gamechatnest.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author: MissingAeon
 * @date: 2024/5/8 14:41
 * @description:
 */
@Profile("test")
@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TestEntityMapper testEntityMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean add(@NotNull TestEntity testEntity) {
        return testEntityMapper.add(testEntity) > 0;
    }

    @Override
    public TestEntity get(Long id) {
        return testEntityMapper.getById(id);
    }

    @Override
    public boolean update(TestEntity testEntity) {
        return testEntityMapper.update(testEntity) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return testEntityMapper.deleteById(id) > 0;
    }

    @Override
    public TestEntity get2(Long id) {
        return (TestEntity) redisTemplate.opsForValue().get(id.toString());
    }

    @Override
    public boolean add2(TestEntity testEntity) {
        redisTemplate.opsForValue().set(testEntity.getId().toString(), testEntity);
        return true;
    }

    @Override
    public boolean update2(TestEntity testEntity) {
        redisTemplate.opsForValue().set(testEntity.getId().toString(), testEntity);
        return true;
    }

    @Override
    public boolean delete2(Long id) {
        redisTemplate.delete(id.toString());
        return true;
    }

    @Override
    public TestEntity get3(Long id) {
        return mongoTemplate.findById(id, TestEntity.class);
    }

    @Override
    public boolean add3(TestEntity testEntity) {
        mongoTemplate.insert(testEntity);
        return true;
    }

    @Override
    public boolean update3(TestEntity testEntity) {
        // 调用方法实现修改
        Query query = new Query(Criteria.where("_id").is(testEntity.getId()));
        Update update = new Update();
        update.set("name", testEntity.getName());
        update.set("age", testEntity.getAge());
        update.set("birthDay", testEntity.getBirthDay());
        // 调用mongoTemplate的修改方法实现修改
        UpdateResult upsert = mongoTemplate.upsert(query, update, TestEntity.class);
        long modifiedCount = upsert.getModifiedCount();// 获取到修改受影响的行数
        return modifiedCount > 0;
    }

    @Override
    public boolean delete3(Long id) {
        Query query = new Query(Criteria.where("_id").is(id));
        DeleteResult remove = mongoTemplate.remove(query, TestEntity.class);
        long deletedCount = remove.getDeletedCount();
        return deletedCount > 0;
    }
}
